<%-- 
    Document   : registration
    Created on : 25 лип 2012, 22:12:25
    Author     : Volodymyr Lavrynovych <volodya.lavrynovych@gmail.com>
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Registration</title>
        
        <script>
            $(document).ready(function(){
                
                var $img = $('<img>').attr('src','http://xopc.net.ua/images/Handshake.png').addClass('img-polaroid span4');
                
                var wizard = new Wizard({
                    id: 'registration',
                    title: 'Registration',
                    buttons: {
                        finish: {
                            onclick: register,
                            title: 'Register'
                        }
                    }
                }).addStep(new Step({
                    id: 'authentication',
                    title: 'Authentication information',
                    description: 'Please, enter your email and password.'
                }).add(new InputControl({
                    id: 'login',
                    placeholder: 'Email',
                    title: 'Email',
                    rules: {
                        required: true,
                        minlength: 5,
                        email: true,
                        isUserUnique: true,
                        messages: {
                            required: "Please enter a login"
                        }
                    }
                })).add(new PasswordControl({
                    id: 'password',
                    placeholder: 'Password', 
                    title: 'Password',
                    rules: {
                        required: true
                    }
                })).add(new InputControl({
                    id: 'confirm_password',
                    type: 'password',
                    placeholder: 'Confirm password',
                    title: 'Confirm password',
                    rules: {
                        required: true,
                        equalTo: "#password",
                        messages: {
                            required: "Please provide a password",
                            equalTo: "Please enter the same password as above"
                        }
                    }
                }))).addStep(new Step({
                    id: 'agreement',
                    title: 'User Agreement'
                }).add(new ControlContainer({
                    id: 'agreement-text',
                    title: 'Agreement'
                }).addElement(new AbstractElement({
                    id: 'agreement-text',
                    value: $img
                }))).add(new InputControl({
                    id: 'accept-agreement',
                    type: 'checkbox',
                    title: 'Accept agreement',
                    rules: {
                        required: true,
                        messages: {
                            required: "Please, accept agreement to finish"
                        }
                    }
                }))).show();
                
                // creates new custom method
                $.validator.addMethod("isUserUnique", isUserUnique, 'User with current email address already exists.');
                
            });
            
            function isUserUnique(login){
                var isUnique;
                
                $.ajax({
                    url: "registration/isUserUnique",
                    data: {
                        login: login
                    },
                    type: "POST",
                    async: false,
                    success: function(result) {
                        isUnique = result;
                    }
                });
                
                return isUnique;
            }
            
            function register(){
                // collects params
                var login = $('#login').val();
                var password = $('#password').val();
                
                if(isUserUnique(login)) {
                    var registrationData = {
                        login: login,
                        password: password
                    }

                    $.ajax({
                        url: "register",
                        data: registrationData,
                        type: "POST",
                        success: function(result) {
                            handleResponse(result);
                        }
                    });
                } else {
                    Notifier.warning('Current login already exists', 'Error!');
                }
            }
        </script>
    </head>
    <body>
    </body>
</html>
